package org.sction.task;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.sction.action.core.ActionUtils;
import org.sction.util.FileUtils;
import org.springframework.jdbc.core.JdbcTemplate;


public class DBackUpTask {
	private DataSource dataSource;
	private String dataBaseName;
	private String backupDir = "";
	public static String DB_BACKUP_DIR = "";;
	private static Logger log = Logger.getLogger(DBackUpTask.class);
	private String path = "";
	private String nameprefix = "";

	public void setPath(String path) {
		this.path = path;
	}

	private String error = "";

	public String getPath() {
		return path;
	}

	public String getError() {
		return error;
	}

	public void restoreMsSql() {
		JdbcTemplate jdbc = new JdbcTemplate(dataSource);
		File file = new File(path);
		if (!(file.exists() && file.isFile())) {
			error = "数据库备份文件" + path + "不存在";
			log.error(error);
			return;
		}
		String sql1 = "";
		try {
			sql1 = "use master;declare @d varchar(8000);set @d='';select @d=@d+' kill '+cast(spid as varchar)+char(13) from master..sysprocesses where dbid=db_id('"
					+ dataBaseName + "');exec(@d);";
			// jdbc.execute(sql1);
		} catch (Exception e) {
		}
		try {
			String sql = sql1 + "use master;restore database " + dataBaseName
					+ " from disk='" + path
					+ "' WITH  FILE = 1, REPLACE,  RECOVERY, STATS = 5;use "
					+ dataBaseName + ";";
			jdbc.execute(sql);
			log.info("数据库还原成功！");
		} catch (Exception ex) {
			error = "数据库还原失败！" + ex.getMessage();
			log.error(error, ex);
			return;
		}
	}

	public void backupMsSql() {
		try {
			if (backupDir == null || backupDir.trim().equals("")) {
				backupDir = DBackUpTask.DB_BACKUP_DIR;
			}
			if (backupDir == null || backupDir.trim().equals("")) {
				backupDir = FileUtils.getFileRoot("") + "work_dir/dbbackup";
			}
			JdbcTemplate jdbc = new JdbcTemplate(dataSource);
			path = backupDir;
			File fileFolder = new File(path);
			if (!(fileFolder.exists() && fileFolder.isDirectory())) {
				fileFolder.mkdirs();
				log.info("数据库备份目录不存在,自动创建目录:" + path);
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
			String time = sdf.format(new Date());
			path += ((path.endsWith("/") || path.endsWith("\\")) ? ""
					: File.separator)
					+ nameprefix
					+ dataBaseName
					+ "_"
					+ time
					+ ".bak";
			String sql = "use master backup database " + dataBaseName
					+ " to disk ='" + path + "' use " + dataBaseName;
			jdbc.execute(sql);
			log.info("数据库自动备份:" + path);

		} catch (Exception ex) {
			error = "数据库自动备份失败！" + ex.getMessage();
			log.error(error, ex);
			return;
		}
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public String getBackupDir() {
		return backupDir;
	}

	public void setBackupDir(String backupDir) {
		this.backupDir = backupDir;
	}

	public String getDataBaseName() {
		return dataBaseName;
	}

	public void setDataBaseName(String dataBaseName) {
		this.dataBaseName = dataBaseName;
	}

	public String getNameprefix() {
		return nameprefix;
	}

	public void setNameprefix(String nameprefix) {
		this.nameprefix = nameprefix;
	}

	public void setDB_BACKUP_DIR(String dB_BACKUP_DIR) {
		DB_BACKUP_DIR = dB_BACKUP_DIR;
	}

}
